/*package com.nd.qa.shareplate.script;

import java.util.ArrayList;
import java.util.List;

import com.nd.qa.shareplate.common.SysConstant;
import com.nd.qa.shareplate.model.PageElement;
import com.nd.qa.shareplate.model.Project;
import com.nd.qa.shareplate.model.Module;
import com.nd.qa.shareplate.model.Page;
import com.nd.qa.shareplate.script.appium.AppiumScriptTool;
import com.nd.qa.shareplate.script.realmonkey.RealMonkeyScriptTool;
import com.nd.qa.shareplate.script.robotium.CreateRobotiumProject;
import com.nd.qa.shareplate.script.robotium.RobotiumScriptTool;
import com.nd.qa.shareplate.service.ProjectService;
import com.nd.qa.shareplate.service.ModuleService;
import com.nd.qa.shareplate.service.PageElementService;
import com.nd.qa.shareplate.service.PageService;

public class GetScriptInfo {

	private ProjectService projectService;
	private ModuleService shareCompService;
	private PageService pageService;
	private PageElementService elementService;
	public void createScript(){
		int projectId=30;
		//1.根据项目id，查询项目信息
		Project project = projectService.queryProjectById(projectId);
		//2.根据项目id，查询模块信息
		List<Module> modulesList = shareCompService.queryModuleByProjectId(projectId);
		//3.获取生成脚本的页面，元素信息
		int flag = Integer.valueOf(project.getProjectType());
		if (flag==SysConstant.ROBOTIUM) {
			//1.1 创建测试工程
			new CreateRobotiumProject().createTestProject(testProjectPath, projectName, packageName, request);
		}
		//调用获取页面，元素的方法
		for (Module module : modulesList) {
			List<List<PageElement>> scriptList = new ArrayList<List<PageElement>>();
			scriptList = getModuleScripts(module);
			if (scriptList == null) {
				return false;
			}
			if (flag == SysConstant.REALMONKEY) {
				RealMonkeyScriptTool pst = new RealMonkeyScriptTool(module,project, projectDirPath);
				pst.generateScript(scriptList);
			}else if (flag == SysConstant.ROBOTIUM) {
				RobotiumScriptTool jst = new RobotiumScriptTool(module,project, projectDirPath);
				jst.generateScript(scriptList);
			}else if (flag == SysConstant.APPIUM) {
				AppiumScriptTool ast = new AppiumScriptTool(module, project, projectDirPath);
				ast.generateScript(scriptList);
			}
		}
		
	}
	*//**
	 * 获取模块的脚本信息
	 * @param module
	 * @param project
	 * @param projectDirPath
	 * @param flag
	 * @return
	 * @throws Exception
	 *//*
	public boolean getCreateScript(Module module,Project project, String projectDirPath, int flag) throws Exception {
		//通过moduleId查询该模块下的最大层级
		int moduleId = module.getModuleId();
		int depth = pageService.queryMaxDepth(moduleId);
		if (depth==-1) {
			return false;
		}
		//查询该模块下层级为0的页面
		int count = 0;
		List<Page> pageList = pageService.queryPagesForDepth(moduleId,0);
		//查询层级为0 的页面是否存在子页面
		for (Page page : pageList) {
			if(!elementService.queryElementForPageId(page.getPageId()).isEmpty()){
				count++;
			}
		}
		if (count==0) {//说明只有apk启动后的首页
			return false;
		}
		//从最高层级开始查询元素的操作信息
		List<List<PageElement>> moduleScriptSet = new ArrayList<List<PageElement>>();
		for (int i = depth; i >=0; i--) {
			List<List<PageElement>> depthScriptSet = new ArrayList<List<PageElement>>();
			//查询每个层级的页面（从最高的层级开始）
			List<Page> endPages = pageService.queryPagesForDepth(moduleId, i);
			if (i == 0) {
				moduleScriptSet.addAll(getScriptList(endPages, moduleScriptSet)) ;
			}
			//遍历同一个层级下各个页面上的元素信息
			for (Page endPage : endPages) {
				List<PageElement> pageElements = elementService.queryElementForPageId(endPage.getPageId());
				//将查询出来的元素信息拷贝一份
				List<PageElement> endElementsCopy = deepCopy(pageElements);
				//删除重复的元素信息
				for (List<PageElement> pageElems : moduleScriptSet) {
					for (PageElement pageElem : endElementsCopy) {
						if (pageElems.contains(pageElem)) {
							System.out.println("equal element");
							pageElements.remove(pageElem);
						}
					}
				}
				//查询pageElements中是否存在子元素或者前置元素，以及该页面的父页面----直至根页面
				for (PageElement pageElement : pageElements) {
					
					List<PageElement> script = new ArrayList<PageElement>();
					//根据该元素id是否是其他元素的前置元素来判断,一直找到没有子元素的元素（即最后操作的元素）
					if (elementService.queryChildElementById(pageElement.getElementId()) != null) {
						continue;
					}
					//从最后一个操作的元素开始反向查询上一次操作的元素 TODO
					script.addAll(getAssociateElements(pageElement));
					depthScriptSet.add(script);
					System.out.println("depthScriptSet===="+depthScriptSet);
				}
			}
			moduleScriptSet.addAll(depthScriptSet);
			System.out.println("moduleScriptSet===="+moduleScriptSet);
		}
		
		if (flag == Util.REALMONKEY) {
			RealMonkeyScriptTool pst = new RealMonkeyScriptTool(module,project, projectDirPath);
			pst.generateScript(moduleScriptSet);
		}else if (flag == Util.ROBOTIUM) {
			RobotiumScriptTool jst = new RobotiumScriptTool(module,project, projectDirPath);
			jst.generateScript(moduleScriptSet);
		}else if (flag == Util.APPIUM) {
			AppiumScriptTool ast = new AppiumScriptTool(module, project, projectDirPath);
			ast.generateScript(moduleScriptSet);
		}
		return true;
	}
	
	*//**
	 * 获取指定模块的所有脚本
	 * 
	 * @param module
	 * @return
	 * @throws Exception 
	 *//*
	private List<List<PageElement>> getModuleScripts(Module module){
		int depth = pageService.queryMaxDepth(module.getModuleId());
		if (depth == -1) {
			return null;
		}
		int count = 0;
		//获取页面深度为0的元素信息
		for (Page page : pageService.queryPagesForDepth(module.getModuleId(), 0)) {
			//判断深度为0的页面是否存在元素信息
			if(!elementService.queryElementForPageId(page.getPageId()).isEmpty()){
				count++;
			} 
		}
		//判断是否创建了页面信息
		if (count==0) {
			return null;
		}
		// 获取模块所有根页面，从深度最大的页面开始遍历出来所含的页面元素信息
		for (int i = depth; i >= 0; i--) {
			// Page rootPage = pages.get(0);
			scriptList.addAll(getScriptForDepth(scriptList, module.getModuleId(), i));
		}
	}
	*//**
	 * 获取指定深度回溯的脚本
	 * 
	 * @param programId--moduleId
	 * @param depth
	 * @param rootPageId
	 * @return
	 * @throws Exception 
	 *//*
	private List<List<PageElement>> getScriptForDepth(
			final List<List<PageElement>> scriptLists, int moduleId, int depth) throws Exception {

		List<List<PageElement>> scriptList = new ArrayList<List<PageElement>>();
		List<Page> endPages = pageService.queryPagesForDepth(moduleId, depth);
		if (depth == 0) {
			return getScriptList(endPages, scriptLists);
		}
		// 回溯页面，组合脚本
		for (Page endPage : endPages) {
			List<PageElement> endElements = searchElements(scriptLists, endPage);
			for (PageElement element : endElements) {
				List<PageElement> script = new ArrayList<PageElement>();
				//传入前置元素id查询
				if (elementService.queryChildElementById(element.getElementId()) != null) {
					continue;
				}
				script.addAll(getAssociateElements(element));
				script.addAll(scriptPath(endPage));
				scriptList.add(script);
			}
		}
		return scriptList;
	}
	
	*//**
	 * 查找根页面上的所有元素并去掉已经使用过的元素
	 * @param scriptLists
	 * @param endPage
	 * @return
	 * @throws Exception 
	 *//*
	private List<PageElement> searchElements(final List<List<PageElement>> scriptLists,
			Page endPage) throws Exception {
		List<PageElement> endElements = elementService.queryElementForPageId(endPage.getPageId());
		List<PageElement> endElementsCopy = deepCopy(endElements);

		// 回溯操作路线
		for (List<PageElement> elems : scriptLists) {
			for (PageElement elem : endElementsCopy) {
				System.out.println(elem.getElementContent()+"|"+elem.getElementOrder());
				System.out.println("-----------");
				if (elems.contains(elem)) {
					System.out.println("equal element");
					endElements.remove(elem);
				}
			}
			
		}
		return endElements;
	}
	
	*//**
	 * 深度复制
	 * @param src
	 * @return
	 *//*
	@SuppressWarnings("unchecked")
	private List<PageElement> deepCopy(List<PageElement> src) {
		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		ObjectOutputStream out;
		try {
			out = new ObjectOutputStream(byteOut);
			out.writeObject(src);
			ByteArrayInputStream byteIn = new ByteArrayInputStream(
					byteOut.toByteArray());
			ObjectInputStream in = new ObjectInputStream(byteIn);
			List<PageElement> dest;
			dest = (List<PageElement>) in.readObject();
			return dest;
		} catch (IOException | ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private List<PageElement> getAssociateElements(PageElement element) throws Exception{
		List<PageElement> elements = new ArrayList<PageElement>();
		elements.addAll(getAllChildElements(element));
		if (Integer.valueOf(element.getElementPrecious()) == 0) {
			return elements;
		}
		elements.addAll(getAllParentElements(element));
		return elements;
	}
	
	*//**
	 * 获取能拼一个脚本的所有元素
	 * 
	 * @param endPage
	 * @param rootPageId
	 * @return
	 * @throws Exception 
	 *//*
	private List<PageElement> scriptPath(Page endPage) throws Exception {
		List<PageElement> script = new ArrayList<PageElement>();
//		ElementOperational elemOperate = iDataService.getElementOperational(endPage
//				.getPageId());
//		int pageId = iDataService.getPageForElement(elemOperate.getElementId());
		PageElement element = elementService.querySingleElementById(endPage.getElementTargetId());
		script.addAll(getAssociateElements(element));
		int pageId = element.getPageId();
		while (pageService.queryDepthForId(pageId) != 0) {
			Page page = pageService.queryPageById(pageId);
			element = elementService.querySingleElementById(page.getElementTargetId());
			pageId = element.getPageId();
			script.addAll(getAssociateElements(element));
		}
		return script;
	}
	
	private List<PageElement> getAllChildElements(PageElement element) throws Exception{
		List<PageElement> elements = new ArrayList<PageElement>();
		List<PageElement> elementsTemp = new ArrayList<PageElement>();
		PageElement elementChild = elementService.queryChildElementById(element.getElementId());
		if (elementChild == null) {
			elements.add(element);
			return elements;		
		}
		while(elementChild != null){
			elements.add(elementChild);
			elementChild = elementService.queryChildElementById(elementChild.getElementId());
		}
		System.out.println("wqm"+elements.size());
		for (int i = elements.size()-1; i >= 0; i--) {
			elementsTemp.add(elements.get(i));
		}
		return elementsTemp;
	}
	
	private List<PageElement> getAllParentElements(PageElement element) throws Exception{
		List<PageElement> elements = new ArrayList<PageElement>();
		PageElement parentElement = elementService.querySingleElementById(Integer.valueOf(element.getElementPrecious()));
		elements.add(parentElement);
		while (Integer.valueOf(parentElement.getElementPrecious()) != 0) {
			parentElement = elementService.querySingleElementById(Integer.valueOf(parentElement.getElementPrecious()));
			elements.add(parentElement);
		}
		return elements;
	}
}
*/